<?php

namespace App\Providers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(
            \D1M\Payment\Service\OrderCheckerInterface::class,
            \App\Services\Payment\OrderChecker::class
        );

        $this->app->singleton(
            \D1M\Payment\Service\OperatorSourceInterface::class,
            \App\Services\Payment\OperatorSource::class
        );

        $this->app->singleton(
            \D1M\Payment\Service\ShouQianBa\StoreInfoSourceInterface::class,
            \App\Services\Payment\ShouQianBa\StoreInfoSource::class,
        );

        $this->app->singleton(
            \D1M\Payment\Service\ShouQianBa\POSInfoSourceInterface::class,
            \App\Services\Payment\ShouQianBa\POSInfoSource::class,
        );

        $this->app->singleton(
            \D1M\Payment\ESBalance\ClientInterface::class,
            \App\Services\Payment\ESBalance\Client::class,
        );

        // 创建组合支付单接口是给 EShop 系统调用，
        // 他们请求过来的数据在原来的数据外边包了一层 data，
        // 所以这里把 CreateCombinedPaymentRequest 替换为改造过的版本。
        $this->app->singleton(
            \D1M\Payment\Http\Requests\CreateCombinedPaymentRequest::class,
            \App\Http\Requests\CreateCombinedPaymentRequest::class
        );

        // 注册用于本地开发的服务
        $this->registerServicesForDevelopment();
    }

    /**
     * 注册用于本地开发的服务
     */
    private function registerServicesForDevelopment()
    {
        if (Config::get('app.env') != 'local') {
            return;
        }

        if (!Config::get('app.debug')) {
            return;
        }

        if (!file_exists(base_path() . '/mock_user.php')) {
            return;
        }

        $this->app->singleton(
            \App\Http\Middleware\AuthLogin::class,
            \Tests\Middleware\AuthLogin::class
        );

        $this->app->singleton(
            \App\Services\Login::class,
            \Tests\Services\Login::class
        );

        $this->app->singleton(
            \App\Http\Middleware\AuthApi::class,
            \Tests\Middleware\AuthApi::class
        );
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

    }
}
